package LINE.comm.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class LINERequestPolicyFilter implements Filter{
	
	private Logger log = Logger.getLogger(this.getClass());
	
	private FilterConfig config;
	
	public void destroy() {
		// TODO Auto-generated method stub
		this.config = null;
	}

	public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest httpRequest = (HttpServletRequest)req;
		
		String context 		= httpRequest.getContextPath();
		String requestUrl	= httpRequest.getServletPath();
		String referUrl 	= httpRequest.getHeader("referer");
		
		log.debug("URL Info start===============================================================");
		log.debug("context : " + context);
		log.debug("requestUrl : " + requestUrl);
		log.debug("referUrl : " + referUrl);
		log.debug("URL Info End===============================================================");
		
		if(requestUrl.indexOf("home.do") < 0){
			if(referUrl == null){ 
				((HttpServletResponse) res).sendRedirect(context + "/view/home.do");
			}else{
				chain.doFilter(req, res);
			}
		}else chain.doFilter(req, res);
	}

	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
		this.config = config;
	}

}
